﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace MDK
{
 //#define DEFAULT_VALUE_STRING "-1"
   class DefaultKeilRegPath
    {
        string REGPATH_UV2FILE = @"UV2FILE\Shell\open\command";
        string REGPATH_UVMPWFILE = @"UVMPWFILE\Shell\open\command";
        string REGPATH_UVPROJFILE = @"UVPROJFILE\Shell\open\command";
        string REGPATH_UVPROJXFILE = @"UVPROJXFILE\Shell\open\command";


        public string UV2FILE;
        public string UVMPWFILE;
        public string UVPROJFILE;
        public string UVPROJXFILE;

        private string ReadReg(string strRegPath)
        {
            string strPathResult = string.Empty;
            string strKeyName = "";     //"(Default)" key, which contains the installed path 
            object objResult = null;

            Microsoft.Win32.RegistryKey regKey = null;
            Microsoft.Win32.RegistryKey regSubKey = null;

            try
            {
                //Read the key 
                regKey = Microsoft.Win32.Registry.ClassesRoot;
                regSubKey = regKey.OpenSubKey(strRegPath, false); //HKEY_CLASSES_ROOT\UV2FILE\Shell\open\command
                objResult = regSubKey.GetValue(strKeyName);
                strPathResult = objResult.ToString();
                string[] vals = strPathResult.Split('\"');
                strPathResult = vals[1];

            }
            catch (System.Security.SecurityException ex)
            {
                throw new System.Security.SecurityException("You have no right to read the registry!", ex);
            }
            catch (Exception ex)
            {
                throw new Exception("Reading registry error!", ex);
            }
            finally
            {

                if (regKey != null)
                {
                    regKey.Close();
                    regKey = null;
                }

                if (regSubKey != null)
                {
                    regSubKey.Close();
                    regSubKey = null;
                }
            }
            return strPathResult;
        }

        public void GetPath()
        {
            UV2FILE = ReadReg(REGPATH_UV2FILE);
            UVMPWFILE = ReadReg(REGPATH_UVMPWFILE);
            UVPROJFILE = ReadReg(REGPATH_UVPROJFILE);
            UVPROJXFILE = ReadReg(REGPATH_UVPROJXFILE);
        }
       /// <summary>
       /// 根据文件名获取相应的UV4的安装路径
       /// </summary>
       /// <param name="FileName">文件名</param>
       /// <param name="MDKPath">MDK的安装路径</param>
       /// <returns>是否成功获取，获取不到则返回错误信息</returns>
        public string GetMDKPath(string FileName)
        {
            string extensions = Path.GetExtension(FileName);
            string MDKPath = "";
            if(String.Equals(extensions,".uv2",StringComparison.CurrentCultureIgnoreCase))
            {
                MDKPath = UV2FILE;
            }
            else if(String.Equals(extensions,".uvproj",StringComparison.CurrentCultureIgnoreCase))
            {
                MDKPath = UVPROJFILE;
            }
            else if (String.Equals(extensions, ".uvprojx", StringComparison.CurrentCultureIgnoreCase))
            {
                MDKPath = UVPROJXFILE;
            }
            return MDKPath;
        }


    }
}
